//
//  BCUserSvipHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/20.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserSvipHeaderView.h"
//
#import "BCUserSvipHeaderNotBuyView.h"
#import "BCUserSvipHeaderContentView.h"


#import "BCUserSvipResponse.h"


@implementation BCUserSvipGradientView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.gradientLayer = [CAGradientLayer layer];
        [self.layer addSublayer:self.gradientLayer];
        self.gradientLayer.startPoint = CGPointMake(0, 0);
        self.gradientLayer.endPoint = CGPointMake(0, 1);
    }
    return self;
}

- (void)setGradientColors:(NSArray<UIColor *> *)gradientColors {
    _gradientColors = gradientColors;
    //
    NSMutableArray *gradientLayerColors = [NSMutableArray arrayWithCapacity:gradientColors.count];
    for (int i=0; i<gradientColors.count; i++) {
        UIColor *color = gradientColors[i];
        [gradientLayerColors addObject:(id)color.CGColor];
    }
    self.gradientLayer.colors = gradientLayerColors;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.gradientLayer.frame = self.bounds;
}

@end

static CGFloat BCUserSvipHeaderViewTopPadding = 20.0f;
static CGFloat BCUserSvipHeaderViewLeftRightPadding = 15.0;
static CGFloat BCUserSvipHeaderViewBottomPadding = 20.0f;

@interface BCUserSvipHeaderView ()

@property (nonatomic, strong) UIView *topView;
/**
 *BCUserSvipGradientView
 */
@property (nonatomic, strong) BCUserSvipGradientView *gradientView;

/**
 *contentView
 */
@property (nonatomic, strong) UIView *contentView;

/**
 *BCUserSvipHeaderNotBuyView
 */
@property (nonatomic, strong) BCUserSvipHeaderNotBuyView *notBuyView;

/**
 *BCUserSvipHeaderContentView
 */
@property (nonatomic, strong) BCUserSvipHeaderContentView *svipContentView;

@end

@implementation BCUserSvipHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.topView = [UIView new];
    self.topView.backgroundColor = [self.class sepColor];
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(5000.0);
        make.bottom.mas_equalTo(self.mas_top);
    }];
    //
    self.gradientView = [BCUserSvipGradientView new];
    self.gradientView.gradientColors = @[[self.class sepColor],[self.class bottomColor]];
    [self addSubview:self.gradientView];
    [self.gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    //
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 10.0;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BCUserSvipHeaderViewTopPadding);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self).offset(-2*BCUserSvipHeaderViewLeftRightPadding);
        make.bottom.mas_equalTo(self).offset(-BCUserSvipHeaderViewBottomPadding);
    }];
    //
    [self setContents];
}
- (void)setContents {
    self.notBuyView = [BCUserSvipHeaderNotBuyView new];
    [self.contentView addSubview:self.notBuyView];
    [self.notBuyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    self.notBuyView.alpha = 0;
    //
    self.svipContentView = [BCUserSvipHeaderContentView new];
    [self.contentView addSubview:self.svipContentView];
    [self.svipContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    self.svipContentView.alpha = 0;
    
}

- (void)setSvipData:(BCUserSvipData *)svipData {
    _svipData = svipData;
    //
    if (svipData.data.has_svip) {
        self.notBuyView.alpha = 0;
        self.svipContentView.alpha = 1;
        self.svipContentView.svipData = svipData;
    }else {
        self.svipContentView.alpha = 0;
        self.notBuyView.alpha = 1;
        self.notBuyView.svipData = svipData;
    }
}

#pragma mark - actions
- (void)addBuyTarget:(id)target action:(SEL)action {
    //action
    [self.notBuyView.buyButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}


+ (CGFloat)heightWithWidth:(CGFloat)width {
    CGFloat height = 0;
    height += BCUserSvipHeaderViewTopPadding;
    //content
    height += 200.0f;
    //
    height += BCUserSvipHeaderViewBottomPadding;
    return height;
}

#pragma mark - 渐变色
+ (UIColor *)topColor {
    return [UIColor qmui_colorWithHexString:@"#4E5260"];
}
+ (UIColor *)sepColor {
    return [UIColor qmui_colorWithHexString:@"#4A4557"];
}
+ (UIColor *)bottomColor {
    return [UIColor qmui_colorWithHexString:@"#262933"];
}

@end
